package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.dialog.MessageBox;
import com.moleq.models.Functions;
import com.moleq.models.SecuritySetting;

public class FunctionService {
	private DatabaseHelper DBHelper;
	String errorString;
	Context mContext;

	public FunctionService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
	}
	public FunctionService(Context context,String dbname) {
		this.DBHelper = new DatabaseHelper(context,dbname);
		mContext = context;
	}

	public List<Functions> getfunctionlist() {
		String sql = "select fs_id,fs_name,fs_order from t_function_setting order by fs_order";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<Functions> Source = new ArrayList<Functions>();
		if (cursor == null) {
			
			return Source;
		}
		while (cursor.moveToNext()) {
			Functions functions = new Functions(cursor.getInt(0),
					cursor.getString(1),cursor.getInt(2));
			Source.add(functions);
		}
		cursor.close();
		return Source;
	}
	
	public List<Functions> getInventoryfunctionlist() {
		String sql = "select fs_id,fs_name,fs_order from t_function_setting where fs_id <> 4 order by fs_order";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<Functions> Source = new ArrayList<Functions>();
		if (cursor == null) {
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return Source;
		}
		while (cursor.moveToNext()) {
			Functions functions = new Functions(cursor.getInt(0),
					cursor.getString(1),cursor.getInt(2));
			Source.add(functions);
		}
		cursor.close();
		return Source;
	}

	public List<Functions> getuserfunctionlist(String ls_user_id) {
		String sql = "select fs_id,fs_name,fs_order from t_function_setting,t_security_setting ";
		String ls_where = "where sc_func_id = fs_id and sc_user_id = "
				+ ls_user_id + " order by fs_order";
		sql = sql + ls_where;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<Functions> Source = new ArrayList<Functions>();
		if (cursor == null) {
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return Source;
		}
		while (cursor.moveToNext()) {
			Functions functions = new Functions(cursor.getInt(0),
					cursor.getString(1),cursor.getInt(2));
			Source.add(functions);
		}
		cursor.close();
		return Source;
	}

	public boolean InsertFunction(int sc_user_id, int sc_func_id) {
		boolean check = true;
		String sql = "insert into t_security_setting values ";
		String ls_where = "(null," + sc_user_id + "," + sc_func_id + ")";
		sql = sql + ls_where;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;
	}

	public boolean DeleteFunction(int sc_user_id, int sc_func_id) {
		boolean check = true;
		String sql = "Delete from t_security_setting ";
		String ls_where = "where sc_func_id = " + sc_func_id
				+ " and sc_user_id = " + sc_user_id;
		sql = sql + ls_where;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;

	}

	public boolean DeleteFunctionbyid(int sc_user_id) {
		boolean check = true;
		String sql = "Delete from t_security_setting ";
		String ls_where = "where sc_user_id = " + sc_user_id;
		sql = sql + ls_where;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		} 
		return check;

	}

	public boolean SelectFunction(int sc_user_id, int sc_func_id) {
		boolean check = false;
		String sql = "select * from t_security_setting where sc_user_id ="
				+ String.valueOf(sc_user_id) + " and sc_func_id ="
				+ String.valueOf(sc_func_id);

		Cursor cursor = DBHelper.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return check;
		}

		if (cursor.moveToNext()) {
			check = true;
		} 
		cursor.close();
		return check;
	}
	
	public List<SecuritySetting> getSecuritySettingById(int sc_user_id) {
		String sql = "select * from t_security_setting where sc_user_id ="
				+ sc_user_id;
		List<SecuritySetting> Source = new ArrayList<SecuritySetting>();
		Cursor cursor = DBHelper.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return Source;
		}

		while (cursor.moveToNext()) {
			SecuritySetting ss = new SecuritySetting(cursor.getInt(0),
					cursor.getInt(1),cursor.getInt(2));
			Source.add(ss);
		}
		cursor.close();
		return Source;
	}
	
	public SecuritySetting FindSecurity(int sc_user_id,int sc_func_id) {
		// TODO Auto-generated method stub
		
		String sql = "select * from t_security_setting where sc_user_id ="
				+ sc_user_id + " and sc_func_id ="
				+ sc_func_id ;
		Cursor cursor = DBHelper.rawQuery(sql, null);
		SecuritySetting mSecuritySetting = null;
		if (cursor == null) {
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return mSecuritySetting;
		}
		while (cursor.moveToNext()) {
			mSecuritySetting = new SecuritySetting(cursor.getInt(0),
					cursor.getInt(1),cursor.getInt(2));
		}
		cursor.close();
		return mSecuritySetting;

	}

	
}
